﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XianboSite.Models;
using XianboSite.Common;

namespace XianboSite.Controllers
{
    public class CommentController : Controller
    {
        //
        // GET: /Comment/

        public ActionResult Index(string liveid)
        {
            ViewData["liveid"] = liveid;
            ViewData["comment"] = DataAccess.GetCommnets(liveid);
            return View();
        }

        [ValidateInput(false)]
        public ActionResult Add(FormCollection colletion)
        {
            string liveid = colletion["liveid"];
            string userid = string.IsNullOrEmpty(colletion["userid"]) ? GetIP() : colletion["userid"];
            string content = colletion["content"];
            DataAccess.AddComment(new UserComment()
            {
                CommentID = RndIDHelper.GetRnd(10, true, true, true, false, string.Empty),
                Content = content,
                LiveID = liveid,
                UserID = userid
            });
            return View();
        }

        public static string GetIP()
        {
            string ip;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            return ip;
        }
    }
}
